﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.Presenters;
using MyCommerce.Presentation.Stores.IViews;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class CashInOutFlowsView : BaseForm, ICashInOutFlowsView
    {
        private CashInOutFlowsPresenter pPresenter;
        public CashInOutFlowsView()
        {
            InitializeComponent();
            pPresenter = new CashInOutFlowsPresenter(this);
        }

        #region ICashInOutFlowsView Members

        public List<DealerStore> ColStores 
        {
            set
            {
                listDealerStore.DisplayMember = "Name";
                listDealerStore.ValueMember = "IdDealerStore";
                listDealerStore.DataSource = value;
            }
        }

        public List<Classifier> ColMoneys
        {
            set 
            {
                listMoney.DisplayMember = "Name";
                listMoney.ValueMember = "IdClassifier";
                listMoney.DataSource = value;
            }
        }

        public List<CashInOutFlows> ColListData
        {
            set 
            {
                if (value != null)
                {
                    gridData.DataSource = value;
                    gridData.DataBind();
                }
            }
        }

        public CashInOutFlows ObjItemSelect
        {
            get
            {
                if (gridData.ActiveRow != null
                    && gridData.ActiveRow.Selected)
                    return (CashInOutFlows)gridData.ActiveRow.ListObject;
                return null;
            }
        }

        public DateTime DateSelect 
        {
            get
            {
                return Convert.ToDateTime(fieldDatePage.Value);
            }
        }

        public CashInOutFlows Get()
        {
            return CashInOutFlows.CreateCashInOutFlows(0, DateTime.Today, fieldObservations.Text,
                Convert.ToDecimal(fieldAmount.Value), Convert.ToInt64(listMoney.SelectedValue),
                Convert.ToBoolean(fieldIsInFlow.CheckedItem.DataValue), Convert.ToInt64(listDealerStore.SelectedValue), Context.UserId);
        }

        public void EnableView(bool action)
        {
            labelContainer1.Enabled = !action;
            gridData.Enabled = !action;

            labelContainer2.Enabled = action;
            labelContainer3.Enabled = action;
            labelContainer4.Enabled = action;
            labelContainer5.Enabled = action;

            commonButtonsToolStrip1.ToggleButtons();
        }

        public void ClearView()
        {
            fieldAmount.Value = 0;
            fieldIsInFlow.CheckedIndex = -1;
            fieldObservations.Text = "";
            listMoney.SelectedIndex = -1;
            if (listDealerStore.Enabled)
                listDealerStore.SelectedIndex = -1;
        }

        public event EventHandler OnSave;
        public event EventHandler OnDelete;
        public event EventHandler OnNew;
        public event EventHandler OnLoadView;
        public event EventHandler OnLoadData;
        public event EventHandler OnCancel;

        #endregion

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCancel != null)
                OnCancel(this, new EventArgs());
        }

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ObjItemSelect != null)
                {
                    if (Context.ShowQuestionMessageBox("¿Desea eliminar este registro?", "Eliminar ingresos y/o salidas de dinero") == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (OnDelete != null)
                        {
                            OnDelete(this, new EventArgs());
                            Context.ShowInformationMessageBox("El registro se eliminó correctamente.", "Eliminar ingresos y/o salidas de dinero");
                        }
                    }
                }
                else
                    Context.ShowInformationMessageBox("Debe seleccionar un registro para poder eliminarlo.", "Eliminar ingresos y/o salidas de dinero");
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnNew != null)
                OnNew(this, new EventArgs());
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateView())
                {
                    if (OnSave != null)
                    {
                        OnSave(this, new EventArgs());
                        Context.ShowInformationMessageBox("El registro se guardó correctamente.", "Ingresos y/o salidas de dinero");
                    }
                }
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private bool ValidateView()
        {
            bool vResult = true;
            StringBuilder message = new StringBuilder();

            if (listDealerStore.SelectedIndex == -1)
            {
                vResult = false;
                message.AppendLine("Debe seleccionar una tienda.");
            }
            if (listMoney.SelectedIndex == -1)
            {
                vResult = false;
                message.AppendLine("Debe seleccionar una moneda.");
            }
            if (Convert.ToDecimal(fieldAmount.Value) <= 0)
            {
                vResult = false;
                message.AppendLine("Debe ingresar un monto mayor a cero.");
            }
            if (string.IsNullOrEmpty(fieldObservations.Text.Trim()))
            {
                vResult = false;
                message.AppendLine("Debe ingresar algún motivo y/u observación.");
            }
            if (fieldIsInFlow.CheckedIndex == -1)
            {
                vResult = false;
                message.AppendLine("Debe seleccionar el tipo de movimiento.");
            }

            if (!vResult)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return vResult;
        }

        private void CashInOutFlowsView_Load(object sender, EventArgs e)
        {
            if (OnLoadView != null)
            {
                OnLoadView(this, new EventArgs());
                if (Context.DealerStoreId > 0)
                {
                    listDealerStore.SelectedValue = Context.DealerStoreId;
                    listDealerStore.Enabled = false;
                }
                else
                    listDealerStore.SelectedIndex = -1;
            }
        }

        private void fieldDatePage_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            if (OnLoadData != null)
                OnLoadData(this, new EventArgs());
        }
    }
}
